package org.example.web.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class TranslateController {
    private static final Logger logger = LoggerFactory.getLogger(TranslateController.class);
    @Autowired
    ObjectMapper objectMapper;

    @RequestMapping("/")
    public ModelAndView index() throws JsonProcessingException {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("html/hello");
        return mav;
    }

    @RequestMapping("/go")
    public ModelAndView translate() throws JsonProcessingException {
        Map<String, Object> data = loadData();
        logger.info(objectMapper.writeValueAsString(data));

        ModelAndView mav = new ModelAndView();
        mav.setViewName("html/user");
        mav.addObject("obj", data);
        return mav;

//        String json = render(TemplateMode.TEXT, "templates/json/", "json-map", "obj", data, "test.json");
//        System.out.println(json);
//
//        String html = render(TemplateMode.HTML, "templates/html/", "user-map", "obj", data, null);
//        System.out.println(html);
    }

    private Map<String, Object> loadData() {
        Map<String, Object> data = new HashMap<>();
        data.put("id", 1001L);
        data.put("name", "张三");
        data.put("url", "http://www.bing.com");
        data.put("createTime", new Date());
        data.put("hobbies", new String[]{"Basketball", "Football", "Pingpang"});
        return data;
    }
}